//
//  ViewController.m
//  数据测试
//
//  Created by 肖棟棟 on 16/9/9.
//  Copyright © 2016年 肖棟棟. All rights reserved.
//

#import "ViewController.h"

#define KCount 200
#define KSWidth [UIScreen mainScreen].bounds.size.width
#define KSHeight [UIScreen mainScreen].bounds.size.height

//每个点之间x坐标的间距
#define baseWidth 37.5

#define KAraryCount 11


#import "NSMutableArray+part.h"
#import "DDTextView1.h"


@interface ViewController ()<UIScrollViewDelegate>{
    NSTimer *timer;
}

@property (nonatomic, strong) NSMutableArray *datas;
@property (nonatomic, strong) NSMutableArray *dataArr;


@property (nonatomic, strong) DDTextView1 *text1;

@property (nonatomic, strong) DDTextView1 *text2;

//记录当前要显示的第一个屏幕数组的数组对应的下表
@property (nonatomic, assign) int index;

//记录当前偏移量
@property (nonatomic, assign) CGFloat offsetX;

//时钟
//@property (nonatomic, strong) NSTimer *timer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //刚开始的偏移量
    self.offsetX = 0;
    
    for (int i = 0; i < 100; ++i) {
        int value = arc4random_uniform(150);
        [self.datas addObject:@(value)];
    }

    
    [self creatFirst];
    
    //开启定时器
//    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(dosomething) userInfo:nil repeats:YES];
//    [timer fire];

}

-(void)dosomething{
    
    //清空数组
    [self.dataArr removeAllObjects];
    
    if (self.text1.yAxisLbArr.count == 10) {
        for (int i = 0; i < 5; ++i) {
            [self.text1.yAxisLbArr removeObjectAtIndex:0];
            //让其滚动到制定位置
        }
  
    }
     int value = arc4random_uniform(150);
     [self.text1.yAxisLbArr addObject:@(value)];
    
//    NSLog(@"%@",self.datas.lastObject);

    [self.text1 setNeedsDisplay];
    
}

-(void)creatFirst{
    
    DDTextView1* text1 = [[DDTextView1 alloc] init];
    self.text1= text1;
    text1.showsHorizontalScrollIndicator = NO;
    text1.delegate = self;
    text1.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:text1];
    text1.frame =CGRectMake(0, 20, KSWidth,150);
    text1.contentSize = CGSizeMake(KCount *[UIScreen mainScreen].bounds.size.width, 0);

    text1.contentOffset = CGPointMake((KCount-1) *[UIScreen mainScreen].bounds.size.width, 0);
    self.dataArr =[NSMutableArray arrayWithArray:[NSMutableArray getPartOfArray:self.datas index:0 length:KAraryCount]];
    
//    NSLog(@"第一次传进去的数组%@,长度%ld",self.dataArr,self.dataArr.count);
    
    text1.yAxisLbArr = [NSMutableArray arrayWithArray:self.dataArr];
}



-(void)creatSecond{
    
    DDTextView1* text2 = [[DDTextView1 alloc] init];
    self.text2= text2;
    text2.showsHorizontalScrollIndicator = NO;
    text2.delegate = self;
    text2.backgroundColor = [UIColor blueColor];
    [self.view addSubview:text2];
    text2.frame =CGRectMake(0, 200, KSWidth,150);
    //
    text2.contentSize = CGSizeMake(KCount *[UIScreen mainScreen].bounds.size.width, 0);
    
    self.dataArr =[NSMutableArray arrayWithArray:[NSMutableArray getPartOfArray:self.datas index:0 length:KAraryCount]];

    text2.yAxisLbArr = self.datas;
}

//全部数据数组
-(NSMutableArray *)datas{
    if (_datas == nil) {
        _datas = [[NSMutableArray alloc] init];
    }
    return _datas;
}

//显示在scrollView上的数组
-(NSMutableArray *)dataArr{
    
    if (_dataArr == nil) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    //数组进行排序
    [self.dataArr sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        if ([obj1 integerValue] > [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedDescending;
        }
        
        if ([obj1 integerValue] < [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    }];
//       NSLog(@"排序后的数组%@",self.dataArr);
    
    //最小的数
    CGFloat minNum =[self yPositionWithyValue:self.dataArr[0]];
    //最大的数
    CGFloat maxNum =[self yPositionWithyValue:self.dataArr[self.dataArr.count-1]];

    NSLog(@"maxNum = %f,minNum = %f",maxNum,minNum);

    //先分出5部分
    //每一份未
//    (maxNum - 150)/5
    
    
    //OK,真正停止了,do something
    //TODO:数据上线变动问题
//    [self.dataArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//
//        //获取Y值
//        CGFloat changeY = [self yPositionWithyValue:self.dataArr[idx]];
//
//        if (changeY>150) {
//            //减速完毕之后 ,添加定时器
//            [self startTimer];
//        }
//        
//    }];
    
    
    
}



//
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
     NSLog(@"停止了才调用");
    
    if (!decelerate) {
        NSLog(@"停止了才调用==========");
        //TODO:数据上线变动问题
        [self.dataArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            //获取Y值
            CGFloat changeY = [self yPositionWithyValue:self.dataArr[idx]];
            
            if (changeY < 0) {
                //清空让后重新绘制
//                [self.dataArr removeAllObjects];
                
                //改变数组
//                self.dataArr =[NSMutableArray arrayWithObjects:@(30),@(120),@(40),@(100),@(10),@(70),@(30),@(100),@(90),@(20),@(110),@(30), nil];
                //动画执行
                
                [UIView animateWithDuration:2 delay:1 options:0 animations:^{
//                    NSLog(@"数据小于0了,重新绘制");
                    [self.text1 setNeedsDisplay];
                } completion:^(BOOL finished) {
                    
                }];
                
            }
        }];
    }

}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
 
    
    CGPoint point=scrollView.contentOffset;
    
    
    //这是不可取的
    if (point.x<=0) {
        return;
    }
    //取整数
    self.index = point.x/baseWidth;
//    NSLog(@"外面的self.index = %d",self.index);
    //清空之前屏幕显示的数组
    [self.dataArr removeAllObjects];
    //重新获取屏幕上显示的数组
    self.dataArr =[NSMutableArray arrayWithArray:[NSMutableArray getPartOfArray:self.datas index:self.index length:KAraryCount]];
    self.text1.yAxisLbArr = [NSMutableArray arrayWithArray:self.dataArr];
    //重新绘制的数组
//    NSLog(@"重新绘制的数组%@,长度%ld",self.dataArr,self.dataArr.count);
    [self.text1 setNeedsDisplay];
}

//获取Y坐标
- (CGFloat)yPositionWithyValue:(NSNumber*)yValue{
    CGFloat y = [yValue floatValue];
    return y;
}


-(void)startTimer{
    //开启时钟
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(verifyCodeTimeCount) userInfo:nil repeats:YES];
    
}

-(void)verifyCodeTimeCount{
    [self.text1 setNeedsDisplay];
    //改变数组
    self.dataArr =[NSMutableArray arrayWithObjects:@(30),@(120),@(40),@(100),@(10),@(70),@(30),@(100),@(90),@(20),@(110),@(30), nil];
}

@end
